global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"
set scheme plotplain
graph set window fontface "Times New Roman"

capture log close
log using "$log/18_worker_separation_table_figures", replace

*Program to plot regressions
cap program drop plot_est
program define plot_est 
args name ytitle ylabel

	*Save estimates
	use "$output/`name'.dta", clear
	tab event_time 
	label define time_lab -12 "Apr 2015" -11 "May 2015" -10 "Jun 2015" ///
		-9 "Jul 2015" -8 "Aug 2015" -7 "Sep 2015" -6 "Oct 2015" -5 "Nov 2015" -4 "Dec 2015" ///
		-3 "Jan 2016" -2 "Feb 2016" -1 "Mar 2016" 0 "Apr 2016" 1 "May 2016" 2 "Jun 2016" ///
		3 "Jul 2016" 4 "Aug 2016" 5 "Sep 2016" 6 "Oct 2016" 7 "Nov 2016" 8 "Dec 2016" ///
		9 "Jan 2017" 10 "Feb 2017" 11 "Mar 2017" 12 "Apr 2017" 13 "May 2017" 14 "Jun 2017" ///
		15 "Jul 2017" 16 "Aug 2017" 17 "Sep 2017" 18 "Oct 2017" 19 "Nov 2017" 20 "Dec 2017" ///
		21 "Jan 2018" 22 "Feb 2018" 23 "Mar 2018" 24 "Apr 2018" 25 "May 2018", replace
		label values event_time time_lab 
		
	*Plot estimates
	graph twoway (connect beta event_time, lc(black) mcolor(black) legend(off)) (rcap ub lb event_time, color(black) legend(off)) , ///
		xline(1 8) xtitle("Month") xlabel(-20(5)20) xlabel(-12(3)25, valuelabel angle(45)) ///
		yline(0) ytitle(`ytitle') ylabel(`ylabel') legend(off)
	graph export "$graphs/`name'.eps", replace
	graph export "$graphs/`name'.jpg", replace	
end 

	plot_est employed_diff_in_diff_estimates0 "Probability of Being Employed"
	plot_est employed_diff_in_diff_estimates1 "Probability of Being Employed"	
	plot_est employed_diff_in_diff_estimates0_dec "Probability of Being Employed"
	plot_est employed_diff_in_diff_estimates1_dec "Probability of Being Employed"	
	plot_est employed_diff_in_diff_estimates0_year "Probability of Being Employed"
	plot_est employed_diff_in_diff_estimates1_year "Probability of Being Employed"	
	plot_est employed_diff_in_diff_estimates0_long_tenure "Probability of Being Employed"
	plot_est employed_diff_in_diff_estimates1_long_tenure "Probability of Being Employed"	
	
**************************************
*Output Table
**************************************
cap program drop save_est
program define save_est 
args name start

	*Save estimates
	use  "$output/`name'.dta", clear
	
	local index = `start'
	forvalues i=1/2 {
		preserve 
		keep if _n==`i'
		
		*Regression Coefficients
		sum beta1
		global b`index'_t0=round(r(mean), 0.0001)
		sum se1 
		global se`index'_t0=round(r(mean),0.0001)
		sum beta2
		global b`index'_t1=round(r(mean), 0.0001)
		sum se2 
		global se`index'_t1=round(r(mean),0.0001)
		sum beta3
		global b`index'_t2=round(r(mean), 0.0001)
		sum se3 
		global se`index'_t2=round(r(mean),0.0001)
		sum beta_change
		global b`index'_change=round(r(mean), 0.0001)
		sum se_change
		global se`index'_change=round(r(mean),0.0001)
		
		*Baseline characteristics
		sum n
		global n`index'=string(r(mean), "%10.0fc")
		
		restore 
		local ++index
	}
	
	sum beta_change if _n==3 
	global baseline`start'=round((1-r(mean))/7, 0.0001)
end 


*Table I
save_est employed_table_estimates 1

	capture erase "$tables/table3.tex"
	file open fh3 using "$tables/table3.tex", write replace

	file write fh3 ///
		"\begin{tabular}{@{\hskip\tabcolsep\extracolsep\fill} l cc cc }" _n ///
		"\toprule" _n ///
		"&& (1) && (2) \\" _n ///
		"\midrule" _n ///
		"Pre-trend && $b1_t0 && $b2_t0   \\" _n ///
		" && ($se1_t0) && ($se2_t0) \\" _n ///
		"[1em] \\" _n ///
		"Anticipation && $b1_t1 && $b2_t1 \\" _n ///
		" && ($se1_t1) && ($se2_t1)  \\" _n ///
		"[1em] \\" _n ///
		"Post-trend && $b1_t2 && $b2_t2   \\" _n ///
		" && ($se1_t2) && ($se2_t2) \\" _n ///
		"[1em] \\" _n ///
		"Change in Slope && $b1_change && $b2_change  \\" _n ///
		" && ($se1_change) && ($se2_change) \\" _n ///
		"\midrule" _n ///
		"Baseline Monthly Separations && $baseline1 && $baseline1 \\" _n ///
		"Worker FE 		  && Y && Y   \\" _n ///
		"Month FE 		  && Y && -  \\" _n ///
		"Firm-Month FE 	  && - && Y  \\" _n ///
		"N && $n1 && $n2  \\" _n ///
		"\bottomrule" _n ///
		"\end{tabular}" _n
		
	file close fh3 
	macro drop fh3 
	

*Table Ib
save_est employed_table_estimates 1
save_est employed_table_estimates_long_tenure 3

	capture erase "$tables/table3b.tex"
	file open fh3 using "$tables/table3b.tex", write replace

	file write fh3 ///
		"\begin{tabular}{@{\hskip\tabcolsep\extracolsep\fill} l cc cc cc cc }" _n ///
		"\toprule" _n ///
		"&& (1) && (2) && (3) && (4) \\" _n ///
		"\midrule" _n ///
		"Pre-trend && $b1_t0 && $b2_t0 && $b3_t0 && $b4_t0   \\" _n ///
		" && ($se1_t0) && ($se2_t0) && ($se3_t0) && ($se4_t0) \\" _n ///
		"[1em] \\" _n ///
		"Anticipation && $b1_t1 && $b2_t1 && $b3_t1 && $b4_t1 \\" _n ///
		" && ($se1_t1) && ($se2_t1) && ($se3_t1) && ($se4_t1)  \\" _n ///
		"[1em] \\" _n ///
		"Post-trend && $b1_t2 && $b2_t2 && $b3_t2 && $b4_t2   \\" _n ///
		" && ($se1_t2) && ($se2_t2) && ($se3_t2) && ($se4_t2) \\" _n ///
		"[1em] \\" _n ///
		"Change in Slope && $b1_change && $b2_change && $b3_change && $b4_change  \\" _n ///
		" && ($se1_change) && ($se2_change)  && ($se3_change) && ($se4_change) \\" _n ///
		"\midrule" _n ///
		"Baseline Monthly Separations && $baseline1 && $baseline1 && $baseline3 && $baseline3 \\" _n ///
		"Worker FE 		  && Y && Y && Y && Y   \\" _n ///
		"Month FE 		  && Y && - && Y && -  \\" _n ///
		"Firm-Month FE 	  && - && Y && - && Y  \\" _n ///
		"N && $n1 && $n2 && $n3 && $n4 \\" _n ///
		"\bottomrule" _n ///
		"\end{tabular}" _n
		
	file close fh3 
	macro drop fh3 
	
	
log close
